<style>
    #authTable + .layui-table-view .layui-table tbody tr:hover {
        background-color: transparent;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="authEdtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="authBtnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="authBtnSync" class="layui-btn icon-btn"><i class="layui-icon">&#xe669;</i>同步
                        </button>
                    </div>
                </div>
            </div>
            <!-- 数据表格 -->
            <table class="layui-table" id="authTable" lay-filter="authTable"></table>
        </div>
    </div>
</div>

<script>
    layui.use(['jquery', 'form', 'table', 'util', 'admin', 'config', 'tableX'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var tableX = layui.tableX;

        // 渲染表格
        table.render({
            elem: '#authTable',
            url: config.base_server + 'authorities',
            where: {
                access_token: config.getToken()
            },
            page: false,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'parentName', sort: true, title: '模块', width: 145},
                {field: 'authorityName', sort: true, title: '接口名称'},
                {field: 'authority', sort: true, title: '权限标识'},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '同步时间', width: 165
                }
            ]],
            done: function (res, curr, count) {
                tableX.merges('authTable', [1], ['parentName']);
            }
        });

        // 搜索按钮点击事件
        $('#authBtnSearch').click(function () {
            var keyword = $('#authEdtSearch').val();
            table.reload('authTable', {where: {keyword: keyword}});
        });

        // 同步按钮点击事件
        $('#authBtnSync').click(function () {
            layer.confirm('确定进行同步吗？<br><span style="color: red;">（自动扫描所有接口生成权限标识）</span>', {
                skin: 'layui-layer-admin'
            }, function () {
                layer.load(2);
                $.get('/v2/api-docs', function (data) {
                    // 解析格式为authorities
                    var authList = new Array();
                    var paths = data.paths;
                    var pathsKeys = Object.keys(paths);
                    var sort = 0;
                    for (var i = 0; i < pathsKeys.length; i++) {
                        var pathKey = pathsKeys[i];
                        var apiObject = paths[pathKey];
                        var apiKeys = Object.keys(apiObject);
                        for (var j = 0; j < apiKeys.length; j++) {
                            var apiKey = apiKeys[j];
                            var methodObject = apiObject[apiKey];
                            var authorities = new Object();
                            authorities.authority = apiKey + ':' + pathKey;
                            authorities.authorityName = methodObject.summary;
                            authorities.parentName = methodObject.tags[0];
                            sort++;
                            authorities.sort = sort;
                            authList.push(authorities);
                        }
                    }
                    // 保存
                    admin.req('authorities/sync', {
                        json: JSON.stringify(authList)
                    }, function (res) {
                        layer.closeAll('loading');
                        if (200 == res.code) {
                            layer.msg(res.msg, {icon: 1});
                            table.reload('authTable');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'POST');
                });
            });
        });

    });
</script>